<?php

		Page::ignore_view();
		$m = new Model('user');
		$data=$m->field('id,un,mendtime,telephone')->list_all_array();
		// dump($data);
		$today=date('Y-m-d');
		// echo $today;
		foreach($data as $v){
			$datetime_start = new \DateTime($v['mendtime']);
			$datetime_end = new \DateTime($today);
			$days = $datetime_start->diff($datetime_end)->days;
			// echo "时间差是:$days";
			if($days<=7){
				$content='您的会员即将在7日内到期！';
				$rr=selectmsg($v['id'],$v['telephone'],$content);
				// $rr=send_sms($v['id'], $v['telephone'], $content);
				//dump($rr);

			}
		}
	

function selectmsg($uid,$to,$content){
		$domain = $_SERVER['HTTP_HOST'];
		$agency = new Model('user_agency');	
		$agency->find(array('domain'=>$domain));
		$agency->message_td=1; //默认走官方配置
		//dump($agency);
		if($agency->message_td==2){//阿里云暂时模板未配置 待开发
			require_once DOXCX_FRAME_PATH . "/fun/aliyun-dysms/sendSms.php";
			$alsms = new Model('user_agency_sms');
			$alsms->find(["uid"=>$agency->id]);
			$accessKeyId = $alsms->accessKeyId;
			$accessKeySecret = $alsms->accessKeySecret;
			$PhoneNumbers = \Request::get('tel');
			$SignName = $alsms->signName;
			$tpls = json_decode($alsms->templateCodes,true);
			$TemplateCode = $tpls['template_register_code'];
			$code = rand(100000,999999);
			$TemplateParam = ["code" =>$code];	
			$resp = sendAlSms($accessKeyId,$accessKeySecret,$PhoneNumbers,$SignName,$TemplateCode,$TemplateParam);
			$smsRecord = new Model('agency_sms_send_record');
			if($resp->Code=='OK'){
				//插入发送短信记录表
				$smsRecord->uid = $agency->id;
				$smsRecord->content = "尊敬的客户您好，您的短信验证码是:".$code;
				$smsRecord->tel = $PhoneNumbers;
				$smsRecord->ctime = time();
				$smsRecord->status = 1;
				$smsRecord->return_code = 'OK';
				$smsRecord->save();
				\Cache::set('sms_code', $code, 300);
				$arr['msg'] = 'ok';
				\Response::json($arr);
			}else{
				//插入发送短信记录表
				$smsRecord->uid = $agency->id;
				$smsRecord->content = "尊敬的客户您好，您的短信验证码是:".$code;
				$smsRecord->tel = $PhoneNumbers;
				$smsRecord->ctime = time();
				$smsRecord->status = 2;
				$smsRecord->return_code = $resp->Code;
				$smsRecord->save();
				$arr['msg'] = $resp->Message;
				\Response::json($arr);
			}
		}else{
			if (!$uid) {
				return false;
			}
			// 准备参数
			//$agency = get_user_agency();
			$user = (new Model('user'))->where(['id' => $uid])->find();
		   if ($user->agid) {
				$agency = (new Model('user_agency'))->find(['id' => $user->agid]);
			} else {
				$agency = (new Model('user_agency'))->find(['isadmin' => 1]);
			}
			if ($user->surplus_sms <= 0) {
				return false;
			}
			//dump($agency);
			$message_sign = $agency->message_sign;
			$raw_content = $content = $content . "【" . $message_sign . "】";
			$message_id = $agency->message_id;
			$message_pwd = $agency->message_pwd;
			$url = "http://service.winic.org:8009/sys_port/gateway/index.asp?";
			$data = "id=%s&pwd=%s&to=%s&Content=%s&time=";
			$id = urlencode(iconv("utf-8", "gb2312", "$message_id"));
			$pwd = $message_pwd;
			$to = $to;
			$content = urlencode(iconv("UTF-8", "GB2312", $content));
			$rdata = sprintf($data, $id, $pwd, $to, $content);
			
			// 发送短信
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $rdata);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			$result = curl_exec($ch);
			curl_close($ch);
			$return = substr($result, 0, 3);
			// 剩余次数-1
			$user->surplus_sms = $user->surplus_sms - 1;
			$user->save();
			// 创建发送记录
			$rec = new Model('sms_send_record');
			$rec->uid = $uid;
			$rec->tel = $to;
			$rec->content = $raw_content;
			$rec->ctime = time();
			$rec->return_code = $return;
			if ($return == '000') {
				// 发送成功
				$rec->status = 1;
				$rec->save();
				return true;
			} else {
				// 发送失败
				$rec->status = 0;
				$rec->save();
				return false;
			}
			
		}

	}
	exit;

?>